package wikitools.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import wikitools.model.Article;
import wikitools.model.TextBase;
import wikitools.model.TextBaseFactory;
import wikitools.util.XMLUtil;

public class UpdateArticle extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        
        String title = request.getParameter("title");
        String content = request.getParameter("content");
        String id = request.getParameter("id");
        System.out.println("the updating artlce: " + id);
        // TODO: Deal with author
        Article article = new Article(id, title, "anonymous", content);
        TextBase db = TextBaseFactory.getTextBase();
        try {
            db.updateArticle(article);
        }catch(Exception e) {
            response.setStatus(404);
            return;
        }
        response.setStatus(201); // resource created
        ServletUtil.writeXML(XMLUtil.toXML(article), response);

    }

}
